/*
 * @Description  : 浏览器
 * @Autor        : 杜康
 * @Date         : 2021-08-14 07:35:50 +0800
 * @LastEditors  : 杜康
 * @LastEditTime : 2021-08-14 22:12:34 +0800
 */

/**
 * @description: 获取窗口可视范围的高度
 * @param {*}
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 07:36:56 +0800
 */
const getClientHeight = () => {
    let clientHeight = 0;
    if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight =
            document.body.clientHeight < document.documentElement.clientHeight
                ? document.body.clientHeight
                : document.documentElement.clientHeight;
    } else {
        clientHeight =
            document.body.clientHeight > document.documentElement.clientHeight
                ? document.body.clientHeight
                : document.documentElement.clientHeight;
    }
    return clientHeight;
};

/**
 * @description: 获取窗口可视范围宽度
 * @param {*}
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 07:37:17 +0800
 */
const getClientWidth = () => {
    let d = document,
        a = d.compatMode == "BackCompat" ? d.body : d.documentElement;
    return a.clientWidth;
};

/**
 * @description: H5软键盘缩回、弹起回调 窗口的文档显示区的高度变化回调
 * 当软件键盘弹起会改变当前 window.innerHeight，监听这个值变化
 * innerheight 返回窗口的文档显示区的高度。
 * innerwidth 返回窗口的文档显示区的宽度。
 * 注意：使用 outerWidth 和 outerHeight 属性获取加上工具条与滚动条窗口的宽度与高度。
 * @param { Function } downCb 当软键盘弹起后，缩回的回调
 * @param { Function } upCb 当软键盘弹起的回调
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 07:37:53 +0800
 */
const onMobileViewportResize = (downCb, upCb) => {
    let clientHeight = window.innerHeight;
    downCb = typeof downCb === "function" ? downCb : function () {};
    upCb = typeof upCb === "function" ? upCb : function () {};
    window.addEventListener("resize", () => {
        let height = window.innerHeight;
        if (height === clientHeight) {
            downCb();
        }
        if (height < clientHeight) {
            upCb();
        }
    });
};

/**
 * @description: 获取浏览器类型和版本
 * @param { String }
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 07:38:40 +0800
 */
const getExplore = () => {
    let sys = {},
        ua = navigator.userAgent.toLowerCase(),
        s;
    (s = ua.match(/rv:([\d.]+)\) like gecko/))
        ? (sys.ie = s[1])
        : (s = ua.match(/msie ([\d\.]+)/))
        ? (sys.ie = s[1])
        : (s = ua.match(/edge\/([\d\.]+)/))
        ? (sys.edge = s[1])
        : (s = ua.match(/firefox\/([\d\.]+)/))
        ? (sys.firefox = s[1])
        : (s = ua.match(/(?:opera|opr).([\d\.]+)/))
        ? (sys.opera = s[1])
        : (s = ua.match(/chrome\/([\d\.]+)/))
        ? (sys.chrome = s[1])
        : (s = ua.match(/version\/([\d\.]+).*safari/))
        ? (sys.safari = s[1])
        : 0;
    // 根据关系进行判断
    if (sys.ie) return "IE: " + sys.ie;
    if (sys.edge) return "EDGE: " + sys.edge;
    if (sys.firefox) return "Firefox: " + sys.firefox;
    if (sys.chrome) return "Chrome: " + sys.chrome;
    if (sys.opera) return "Opera: " + sys.opera;
    if (sys.safari) return "Safari: " + sys.safari;
    return "Unkonwn";
};

/**
 * @description: 获取操作系统类型
 * @param {*}
 * @return { String }
 * @author: 杜康
 * @Date: 2021-08-14 07:39:29 +0800
 */
const getOS = () => {
    let userAgent =
        ("navigator" in window &&
            "userAgent" in navigator &&
            navigator.userAgent.toLowerCase()) ||
        "";
    let vendor =
        ("navigator" in window &&
            "vendor" in navigator &&
            navigator.vendor.toLowerCase()) ||
        "";
    let appVersion =
        ("navigator" in window &&
            "appVersion" in navigator &&
            navigator.appVersion.toLowerCase()) ||
        "";

    if (
        /iphone/i.test(userAgent) ||
        /ipad/i.test(userAgent) ||
        /ipod/i.test(userAgent)
    )
        return "ios";
    if (/android/i.test(userAgent)) return "android";
    if (/win/i.test(appVersion) && /phone/i.test(userAgent))
        return "windowsPhone";
    if (/mac/i.test(appVersion)) return "MacOSX";
    if (/win/i.test(appVersion)) return "windows";
    if (/linux/i.test(appVersion)) return "linux";
};

/**
 * @description: 判断是否ie浏览器
 * @param {*}
 * @return { Boolean }
 * @author: 杜康
 * @Date: 2021-08-14 07:40:16 +0800
 */
const isIE = () => {
    if (!!window.ActiveXObject || "ActiveXObject" in window) {
        return true;
    } else {
        return false;
    }
};

/**
 * @description: 判断浏览器是否支持webP格式图片
 * @param {*}
 * @return { Boolean }
 * @author: 杜康
 * @Date: 2021-08-14 07:40:52 +0800
 */
const isSupportWebP = () => {
    return (
        !![].map &&
        document
            .createElement("canvas")
            .toDataURL("image/webp")
            .indexOf("data:image/webp") == 0
    );
};

/**
 * @description: 获取窗口尺寸
 * @param {*}
 * @return { Object }
 * @author: 杜康
 * @Date: 2021-08-14 07:41:20 +0800
 */
const getClientSize = () => {
    // 包含滚动条
    if (window.innerWidth) {
        return {
            width: window.innerWidth,
            height: window.innerHeight,
        };
    } else {
        // ie8及其以下
        if (document.compatMode === "BackCompat") {
            // 怪异模式
            return {
                width: document.body.clientWidth,
                height: document.body.clientHeight,
            };
        } else {
            // 标准模式 不包含滚动条 【一般常用】 与 jQuery的$(window).height()一致
            return {
                width: document.documentElement.clientWidth,
                height: document.documentElement.clientHeight,
            };
        }
    }
};

/**
 * @description: 开启全屏
 * @param {*} element document.body
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 07:46:21 +0800
 */
const launchFullscreen = (element = document.body) => {
    if (element.requestFullscreen) {
        element.requestFullscreen();
    } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
    } else if (element.msRequestFullscreen) {
        element.msRequestFullscreen();
    } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullScreen();
    }
};

/**
 * @description: 关闭全屏
 * @param {*}
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 07:46:38 +0800
 */
const exitFullscreen = () => {
    if (document.exitFullscreen) {
        document.exitFullscreen();
    } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
    }
};

/**
 * @description: 打开一个窗口
 * @param { String } url
 * @param { String } windowName
 * @param { Number } width
 * @param { Number } height
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 07:47:17 +0800
 */
const openWindow = (url, windowName, width, height) => {
    let x = parseInt(screen.width / 2.0) - width / 2.0;
    let y = parseInt(screen.height / 2.0) - height / 2.0;
    let isMSIE = navigator.appName == "Microsoft Internet Explorer";
    if (isMSIE) {
        let p = "resizable=1,location=no,scrollbars=no,width=";
        p = p + width;
        p = p + ",height=";
        p = p + height;
        p = p + ",left=";
        p = p + x;
        p = p + ",top=";
        p = p + y;
        window.open(url, windowName, p);
    } else {
        let win = window.open(
            url,
            "ZyiisPopup",
            "top=" +
                y +
                ",left=" +
                x +
                ",scrollbars=" +
                scrollbars +
                ",dialog=yes,modal=yes,width=" +
                width +
                ",height=" +
                height +
                ",resizable=no"
        );
        eval("try { win.resizeTo(width, height); } catch(e) { }");
        win.focus();
    }
};

/**
 * @description: 禁止鼠标右键、选择、复制
 * @param {*}
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 07:48:09 +0800
 */
const stopContextmenuAndSelectAndCopy = () => {
    ["contextmenu", "selectstart", "copy"].forEach((ev) => {
        document.addEventListener(ev, (event) => {
            return (event.returnValue = false);
        });
    });
};

/**
 * @description: 自适应页面（rem）
 * @param { Number } width
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 07:49:05 +0800
 */
const autoResponse = (width = 750) => {
    const target = document.documentElement;
    target.clientWidth >= 750
        ? (target.style.fontSize = "100px")
        : (target.style.fontSize = (target.clientWidth / width) * 100 + "px");
};

export default {
    onMobileViewportResize,
    getExplore,
    getOS,
    isIE,
    isSupportWebP,
    getClientHeight,
    getClientWidth,
    getClientSize,
    launchFullscreen,
    exitFullscreen,
    openWindow,
    stopContextmenuAndSelectAndCopy,
    autoResponse,
};
